/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.AdminData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import database.*;

/**
 *
 * @author brlnt
 */
public class LoginAdminservlet extends HttpServlet {

    private String ID,username, password, label;
    LoginDB admindb;
    AdminData dataAdmin = new AdminData();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String adminPath = request.getServletPath();
        HttpSession session = request.getSession();
        if (adminPath.equals("/administrator")) {
            if (session.getAttribute("adminMasuk") == null) {
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            } else {
                AdminData ud = (AdminData) session.getAttribute("adminMasuk");
                    request.setAttribute("title", "Home | ");
                    RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/home_admin.jsp");
                    rd.forward(request, response);
                
            }
        } else if(adminPath.equals("/keluar")){
            session.removeAttribute("adminMasuk");
             RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
             rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="expand" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // pembuatan session
        HttpSession session = request.getSession();
        // pembuatan objek
        admindb = new LoginDB();
        ID = request.getParameter("ID");
        username = request.getParameter("username");
        password = request.getParameter("password");
        label = request.getParameter("label");

        try {
            // pengecekan login
            if (admindb.cekLoginAdmin(ID, username, password, label)) {
                dataAdmin = admindb.getAdminById(ID);
                session.setAttribute("adminMasuk", dataAdmin);
                request.setAttribute("title", "Home | ");
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/home_admin.jsp");
                rd.forward(request, response);
            } else { // autentikasi gagal
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        } catch (SQLException e) {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
